package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Role;
import org.ahuanlien.crm.query.QueryObject;
import org.ahuanlien.crm.service.IRoleService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@RequestMapping("view")
	public String view() {
		return "role/view";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(QueryObject qo) {
		PageResult result = roleService.query(qo);
		return result;
	}
	@RequestMapping("selectAll")
	@ResponseBody
	public List<Role> selectAll() {
		List<Role> roles = roleService.selectAll();
		return roles;
	}
	@RequestMapping("getRoleIdByEmployeeId")
	@ResponseBody
	public List<Long> getRoleIdByEmployeeId(Long employeeId) {
		List<Long> roleIds = roleService.getRoleIdByEmployeeId(employeeId);
		return roleIds;
	}

	@RequestMapping("getMenuIdByRoleId")
	@ResponseBody
	public List<Long> getMenuIdByRoleId(Long roleId) {
		List<Long> menuId = roleService.getMenuIdByRoleId(roleId);
		return menuId;
	}
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Role role) {
		try {
			roleService.saveOrUpdate(role);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			roleService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
}
